import { Modal as AntModal } from "antd";
import React, { forwardRef, useImperativeHandle, useState } from "react";
import { type CommonComponentProps } from "../Page/dev";

export interface ModalRef {
    open: () => void
    close: () => void
}

const Modal: React.ForwardRefRenderFunction<ModalRef, Omit<CommonComponentProps, "ref">> = ({ children, title, onOk, onCancel, styles }, ref) => {
    const [open, setOpen] = useState<boolean>(false);


    useImperativeHandle(ref, () => {
        return {
            open: () => {
                setOpen(true)
            },
            close: () => {
                setOpen(false)
            }
        }
    })

    return (
        <AntModal title={title}
            style={styles}
            open={open}
            onCancel={() => {
                onCancel && onCancel();
                setOpen(false);
            }}
            onOk={() => {
                onOk && onOk()
            }}
            destroyOnClose>
            {children}
        </AntModal>
    )
}

export default forwardRef(Modal)